import React, { Component } from 'react'
import {ListBox} from '../../../components'
import UpdateWorkareaPage from './UpdateNew'
import UpdateWorkareaPageUp from './UpdateNewUp'

import {fetchData} from '../../../components/AjaxConsumer'
import {  Button, Modal, Form, Input, Radio,Upload, Icon } from 'antd';
export default class ListProOrderInSnPage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            tableColumn: {},
            searchComponentData: [],
            visible: false,
        };
        //行操作列表
        this.rowOperation = [
            {
                handleName: 'edit',
                iconClass: 'cellEdit'
            }, {
                handleName: 'delete',
                iconClass: 'cellDele'
            }, {
                handleName: 'assignUser',
                iconClass: 'cellRole'
            }
        ];
        //header 操作列表
        this.headerOperation = [
            //{
            //    handleName: 'add',
            //    showName: '新增',
            //    iconClass: 'plus',
            //    permission: 'rawbarcode-add'
            //},
            {
               handleName: 'edit',
               showName: '修改',
               iconClass: 'edit',
               permission: 'probarcode-edit'
            }
            //,{
            //    handleName: 'delete',
            //    showName: '删除',
            //    iconClass: 'delete',
            //    permission: 'rawbarcode-del'
            //},{
            //    handleName: 'upload',
            //    showName: '打印',
            //    iconClass: 'upload',
            //    permission: 'rawbarcode-print'
            //}
            //  ,{
            //     handleName: 'printBarCodeAll',
            //     showName: '批量条码打印',
            //     iconClass: 'print',
            //     permission: 'printBarCode-all'
            // }
            ,
            {
                handleName: 'printBarCode',
                showName: '合格证打印',
                iconClass: 'print',
                permission: 'into-printBarCode'
            }
        ];

        //table列表展示列配置
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏
        this.tableColumn = {
            storageKey: 'orderInSnId',
            data: [
                {
                    showName: '批次号',
                    columnName: 'userDefined2',
                    columnWidth: 120
                }, {
                    showName: '条码',
                    columnName: 'barcode',
                    columnWidth: 130
                }, {
                    showName: '型号',
                    columnName: 'model',
                    columnWidth: 120
                }, {
                    showName: '品名',
                    columnName: 'productName',
                    columnWidth: 90
                }, {
                    showName: '定量',
                    columnName: 'density',
                    columnWidth: 90
                }, {
                    showName: '规格',
                    columnName: 'spec',
                    columnWidth: 90
                }, {
                    showName: '生产机号',
                    columnName: 'machineNumber',
                    columnWidth: 110,
                    showOption:[
                    ]
                }, {
                    showName: '等级',
                    columnName: 'grade',
                    columnWidth: 90,
                    showOption:[
                        {
                            optionKey:'',
                            optionShow:''
                        }, {
                            optionKey:'1',
                            optionShow:'一等品'
                        }, {
                            optionKey:'2',
                            optionShow:'二等品'
                        }, {
                            optionKey:'3',
                            optionShow:'处理品'
                        }
                    ]
                }, {
                    showName: '级别',
                    columnName: 'level',
                    columnWidth: 90,
                    showOption:[
                        {
                            optionKey:'',
                            optionShow:''
                        }, {
                            optionKey:'A',
                            optionShow:'A'
                        }, {
                            optionKey:'B',
                            optionShow:'B'
                        }, {
                            optionKey:'C',
                            optionShow:'C'
                        }, {
                            optionKey:'D',
                            optionShow:'D'
                        }
                    ]
                // }, {
                //     showName: '类型',
                //     columnName: 'type',
                //     columnWidth: 90,
                //     showOption:[
                //         {
                //             optionKey:'',
                //             optionShow:''
                //         }, {
                //             optionKey:'0',
                //             optionShow:'正品'
                //         }, {
                //             optionKey:'1',
                //             optionShow:'样品'
                //         }
                //     ]
                // }, {
                    // showName: '打印状态',
                    // columnName: 'status',
                    // columnWidth: 110,
                    // showOption:[
                    //     {
                    //         optionKey:'',
                    //         optionShow:''
                    //     }, {
                    //         optionKey:'0',
                    //         optionShow:'未打印'
                    //     }, {
                    //         optionKey:'1',
                    //         optionShow:'已打印'
                    //     }
                    // ]
                }, {
                    showName: '入库状态',
                    columnName: 'userDefined1',
                    columnWidth: 110,
                    showOption:[
                    {
                        optionKey:'',
                        optionShow:''
                    }, {
                        optionKey:'0',
                        optionShow:'未入库'
                    }, {
                        optionKey:'1',
                        optionShow:'已入库'
                    }
                ]
                }, {
                    showName: '下线时间',
                    columnName: 'gmtCreate',
                    columnWidth: 140
                }, {
                    showName: '生产条码方式',
                    columnName: 'userDefined4',
                    columnHidden: true,
                    foreverHidden: true,
                    columnWidth: 130,
                    showOption:[
                        {
                            optionKey: 'DJ',
                            optionShow: '登记'
                        }
                    ]
                }, {
                    showName: '创建人',
                    columnHidden: true,
                    columnName: 'createBy'
                }
                // , {
                //     showName: '生产类型',
                //     columnName: 'userDefined10',
                //     columnWidth: 110,
                //     showOption: []
                // }
                // {
                //     showName: '登记表主键',
                //     columnName: 'userDefined3',
                //   //columnWidth: 140
                // },{
                //     showName: '是否合格',
                //     columnName: 'userDefined6',
                //     columnWidth: 100,
                //   showOption:[
                //     {
                //         optionKey:'',
                //         optionShow:''
                //     },{
                //         optionKey:'0',
                //         optionShow:'不合格'
                //     },{
                //         optionKey:'1',
                //         optionShow:'合格'
                //     }
                // ]
                // },{
                //     showName: '不合格原因',
                //     columnName: 'userDefined7',
                //     columnWidth: 140
                // }
            ]
        };

        //搜索属性配置
        this.searchComponentData = [
            {
                showName: '等级',
                name: 'grade',
                type: 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'1',
                        optionShow:'一等品'
                    },{
                        optionKey:'2',
                        optionShow:'二等品'
                    },{
                        optionKey:'3',
                        optionShow:'处理品'
                    }
                ]
            },{
                showName: '级别',
                name: 'level',
                type: 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'A',
                        optionShow:'A'
                    },{
                        optionKey:'B',
                        optionShow:'B'
                    },{
                        optionKey:'C',
                        optionShow:'C'
                    },{
                        optionKey:'D',
                        optionShow:'D'
                    }
                ]
            },{
            //     showName: '类型',
            //     name: 'type',
            //     type: 'select',
            //     optionList:[
            //         {
            //             optionKey:'',
            //             optionShow:''
            //         },{
            //             optionKey:'0',
            //             optionShow:'正品'
            //         },{
            //             optionKey:'1',
            //             optionShow:'样品'
            //         }
            //     ]
            // },{
                showName: '下线时间',
                name:'gmtCreate',
                type: 'date',
                maxName:'gmtCreateMax',
                minName:'gmtCreateMin',
                showTime: '1'
            },{
                showName: '打印状态',
                name:'status',
                type: 'select',
                optionList:[//下拉框的信息
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'0',
                        optionShow:'未打印'
                    },{
                        optionKey:'1',
                        optionShow:'已打印'
                    }
                ]
            },{
                showName: '入库状态',
                name:'userDefined1',
                type: 'select',
                optionList:[//下拉框的信息
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'0',
                        optionShow:'未入库'
                    },{
                        optionKey:'1',
                        optionShow:'已入库'
                    }
                ]
            },{
                showName: '是否合格',
                name:'userDefined6',
                type: 'select',
                optionList:[//下拉框的信息
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'0',
                        optionShow:'不合格'
                    },{
                        optionKey:'1',
                        optionShow:'合格'
                    }
                ]
            },
            {
                showName: '生产机号',
                name: 'machineNumber',
                type: 'select',
                optionList:[]
                // url:'/base/dic/get/machine_number'
            }
            // ,{
            //     showName: '生产类型',
            //     name: 'userDefined10',
            //     type: 'select',
            //     optionList: [
            //         {
            //             optionKey: '',
            //             optionShow: ''
            //         },{
            //             optionKey:'A',
            //             optionShow:'A'
            //         },{
            //             optionKey:'B',
            //             optionShow:'B'
            //         }
            //     ]
            // }
        ];

    }
    
    componentWillMount() {
        const dicType = 'machine_number';
        const options= {
            url: '/base/dic/get/' + dicType,
            type: 'get',
            condition:{}
        };
        //先写死，后面再改
        fetchData(this, options, this.dicInitCallBackHandle);
    }

    //下拉框 初始化
    dicInitCallBackHandle = (resData)=>{
        //alert('resData' + JSON.stringify(resData, null, 4));
        let optionList = [];
        let tmp = {};
        tmp.optionKey = '';
        tmp.optionShow = '';
        optionList.push(tmp);
        resData.data.map( (e,index) => {
            let tmp = {};
            tmp.optionKey = Number(e.dicCode);
            tmp.optionShow = e.dicName;
            optionList.push(tmp);
        });

        //alert('optionList' + JSON.stringify(optionList, null, 4));
        this.tableColumn.data[6].showOption = optionList;
        this.searchComponentData[6].optionList = optionList;
    }

    rowHandles = (strHandles, rowObj) => {
        console.log("strHandles===ListResource=======", strHandles);
        if (strHandles === 'edit') {
            this.editHandle(rowObj);
        } else if (strHandles === 'delete') {
            this.deleteHandle(rowObj);
        } else if (strHandles === 'add') {
            this.addHandle();
        } else if (strHandles == 'upload') {
            this.upload();
        }else if(strHandles == 'printBarCodeAll'){
            this.printBarCodeHandle(rowObj);
        }else if(strHandles == 'printBarCode'){
            this.printHandle(rowObj);
        }
    }
    // 增加工作区
    addHandle = () => {
        console.log("====执行了新增工作区操作====");
        this.refs.updateWorkarea.addWorkareaHandle();
    }

    // 上传
    upload = () => {
        console.log("====执行了上传操作====");
                //    this.setState({ visible: true });
        this.refs.updateWorkareaUp.add();
                


    }
    editHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            this.refs.updateWorkarea.editHandle(rowObj[0].data);
        }
    }

    //批量条码打印
    printBarCodeHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            
            // this.refs.printInfo.printHandle(rowObj);
            const userDefined2 = rowObj[0].data.userDefined2;
            const win = window.open("/wms/PrintBarCode?userDefined2="+userDefined2,"_blank");
            win.focus();

        }
    };

    //补码打印
    printHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据'
            });
        }else {
            const barcodes = [];
            for (const item of rowObj) {
                if(item.data.userDefined1 =='0'){
                    Modal.error({
                        title: '错误提示',
                        content: '未入库条码不能打印！'
                    });
                    return false;
                }
                barcodes.push(item.data.barcode)
            }
            const win = window.open("/wms/NewPrintBarCode?barcodes="+barcodes.join("-"),"_blank");
            win.focus();
        }
    }

    deleteHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            this.refs.updateWorkarea.deleteHandle(rowObj[0].data)
        }
    }
    // 给区域分配角色
    assignFactoryUserHandle = (rowObj) => {
        let workareaId = rowObj.workareaId;
        this.refs.assignFactoryUser.initModal(workareaId);
    }


    //刷新列表
    refreshHandle = () => {
        this.refs.listProOrderInSn.refreshHandle();
    }

  handleCancel = () => {
    this.setState({ visible: false });
      this.form.resetFields();
    
  }
  handleCreate = () => {
    const form = this.form;
    form.validateFields((err, values) => {
      if (err) {
        return;
      }

      console.log('Received values of form: ', values);
      form.resetFields();
      this.setState({ visible: false });
    });
  }
  saveFormRef = (form) => {
    this.form = form;
  }


    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;

        return (
            <div>
                <ListBox
                    ref='listProOrderInSn'
                    title="成品下线明细"
                    isHideLeft={isHideLeft}
                    tableColumnData={this.tableColumn}
                    searchComponentData={this.searchComponentData}
                    url='/product/probarcode/list?userDefined4=DJ'
                    rowHandles={this.rowHandles}
                    rowOperation={this.rowOperation}
                    headerOperation={this.headerOperation}
                    // isRowSingle = "false"
                />

                <UpdateWorkareaPage
                    ref="updateWorkarea"
                    refreshHandle={this.refreshHandle}
                />
                 <UpdateWorkareaPageUp
                    ref="updateWorkareaUp"
                    refreshHandle={this.refreshHandle}
                /> 
            
            </div>
        )
    }
}
